package com.kitty.game.player.serializer;

import com.kitty.game.pet.model.PetBox;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;

public class PetBoxSerializer implements IPlayerPropSerializer {

    @Override
    public void serialize(Role player) {
        if (player.getPetBox() != null) {
            player.setPetBoxJson(JsonUtils.object2String(player.getPetBox()));
        }
    }

    @Override
    public void deserialize(Role player) {
        String json = player.getPetBoxJson();
        if (StringUtils.isNotEmpty(json)) {
            player.setPetBox(JsonUtils.string2Object(json, PetBox.class));

            /*ServerConfig config = SpringUtils.getBean(ServerConfig.class);
            if (config.getServerId() == 1) {
                // 修复宠物id
                Map<Integer, Pet> allPets = player.getPetBox().getAllPets();

                Map<Integer, Pet> tmpPets = new HashMap<>();
                Map<Integer, Integer> mapper = new HashMap<>();

                for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
                    Pet pet = entry.getValue();
                    if (!pet.isRepairedId()) {
                        int oldId = pet.getId();
                        int newId = SpringUtils.getIdentityService().getNextId(IdentityType.ROLE);
//                        pet.setRepairedId(true);
                        pet.setId(newId);
                        mapper.put(oldId, newId);
                        pet.setInStore(false);
                    }

                    tmpPets.put(pet.getId(), pet);

                    for (Map.Entry<Integer, PetBook> tmp : player.getPetBox().getBooks().entrySet()) {
                        PetBook book = tmp.getValue();
                        if (mapper.containsKey(book.getPetId())) {
                            book.setPetId(mapper.get(book.getPetId()));
                        }
                    }
                }
                player.getPetBox().setAllPets(tmpPets);
            }*/

        }
    }
}
